home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / etc / init.d / alsa-utils < prev    next >
Encoding:
Text File  |  2007-02-18  |  9.3 KB  |  379 lines

  1. #!/bin/sh
  2. #
  3. # alsa-utils initscript
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides:          alsa-utils
  7. # Required-Start:    $local_fs
  8. # Required-Stop:     $local_fs
  9. # Should-Start:
  10. # Should-Stop:
  11. # Default-Start:     S
  12. # Default-Stop:      0 6
  13. # Short-Description: Restore and store ALSA driver settings
  14. # Description:       This script stores and restores mixer levels on
  15. #                    shutdown and bootup.On sysv-rc systems: to
  16. #                    disable storing of mixer levels on shutdown,
  17. #                    remove /etc/rc[06].d/K50alsa-utils.  To disable
  18. #                    restoring of mixer levels on bootup, rename the
  19. #                    "S50alsa-utils" symbolic link in /etc/rcS.d/ to
  20. #                    "K50alsa-utils".
  21. ### END INIT INFO
  22.  
  23. # Don't use set -e; check exit status instead
  24.  
  25. # Exit silently if package is no longer installed
  26. [ -x /sbin/alsactl ] || exit 0
  27.  
  28. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  29. MYNAME=/etc/init.d/alsa-utils
  30.  
  31. . /lib/lsb/init-functions
  32.  
  33. # $1 EXITSTATUS
  34. # [$2 MESSAGE]
  35. log_action_end_msg_and_exit()
  36. {
  37.     log_action_end_msg "$1" ${2:+"$2"}
  38.     exit $1
  39. }
  40.  
  41. # $1 PROGRAM
  42. executable()
  43. {
  44.     # If which is not available then we must be running before
  45.     # /usr is mounted on a system that has which in /usr/bin/.
  46.     # Conclude that $1 is not executable.
  47.     [ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
  48.     which "$1" >/dev/null 2>&1
  49. }
  50.  
  51. # Wait for filesystems to show up
  52. while [ ! -d /usr/bin -o ! -d /var/lib/alsa ]; do
  53.     sleep 0.2
  54. done
  55.  
  56. executable amixer || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }
  57.  
  58. bugout() { echo "${MYNAME}: Programming error" >&2 ; exit 123 ; }
  59.  
  60. # $1 <card ID> | "all"
  61. restore_levels()
  62. {
  63.     [ -f /var/lib/alsa/asound.state ] || return 1
  64.     CARD="$1"
  65.     [ "$1" = all ] && CARD=""
  66.     # Assume that if alsactl prints a message on stderr
  67.     # then it failed somehow.  This works around the fact
  68.     # that alsactl doesn't return nonzero status when it
  69.     # can't restore settings for the card
  70.     if MSG="$(alsactl restore $CARD 2>&1 >/dev/null)" && [ ! "$MSG" ] ; then
  71.         return 0
  72.     else
  73.         # Retry with the "force" option.  This restores more levels
  74.         # but it results in much longer error messages.
  75.         alsactl -F restore $CARD >/dev/null 2>&1
  76.         [ -z "$CARD" ] && log_action_cont_msg "warning: 'alsactl restore' failed with error message '$MSG'"
  77.         return 1
  78.     fi
  79. }
  80.  
  81. # $1 <card ID> | "all"
  82. store_levels()
  83. {
  84.     CARD="$1"
  85.     [ "$1" = all ] && CARD=""
  86.     if MSG="$(alsactl store $CARD 2>&1)" ; then
  87.         sleep 1
  88.         return 0
  89.     else
  90.         [ -z "$CARD" ] && log_action_cont_msg "warning: 'alsactl store' failed with error message '$MSG'"
  91.         return 1
  92.     fi
  93. }
  94.  
  95. echo_card_indices()
  96. {
  97.     if [ -f /proc/asound/cards ] ; then
  98.         sed -n -e's/^[[:space:]]*\([0-7]\)[[:space:]].*/\1/p' /proc/asound/cards
  99.     fi
  100. }
  101.  
  102. filter_amixer_output()
  103. {
  104.     sed \
  105.         -e '/Unable to find simple control/d' \
  106.         -e '/Unknown playback setup/d' \
  107.         -e '/^$/d'
  108. }
  109.  
  110. # The following functions try to set many controls.
  111. # No card has all the controls and so some of the attempts are bound to fail.
  112. # Because of this, the functions can't return useful status values.
  113.  
  114. # $1 <control>
  115. # $2 <level>
  116. # $CARDOPT
  117. unmute_and_set_level()
  118. {
  119.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  120.     amixer $CARDOPT -q set "$1" "$2" unmute 2>&1 | filter_amixer_output || :
  121.     return 0
  122. }
  123.  
  124. # $1 <control>
  125. # $CARDOPT
  126. mute_and_zero_level()
  127. {
  128.     { [ "$1" ] && [ "$CARDOPT" ] ; } || bugout
  129.     amixer $CARDOPT -q set "$1" "0%" mute 2>&1 | filter_amixer_output || :
  130.     return 0
  131. }
  132.  
  133. # $1 <control>
  134. # $2 "on" | "off"
  135. # $CARDOPT
  136. switch_control()
  137. {
  138.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  139.     amixer $CARDOPT -q set "$1" "$2" 2>&1 | filter_amixer_output || :
  140.     return 0
  141. }
  142.  
  143. # $1 <card ID>
  144. sanify_levels_on_card()
  145. {
  146.     CARDOPT="-c $1"
  147.  
  148.     unmute_and_set_level "Master" "80%"
  149.     unmute_and_set_level "Master Mono" "80%"      # Debian #406047
  150.     unmute_and_set_level "Master Digital" "80%"   # E.g., cs4237B
  151.     unmute_and_set_level "Playback" "80%"
  152.     unmute_and_set_level "Headphone" "70%"
  153.     unmute_and_set_level "PCM" "80%"
  154.     unmute_and_set_level "PCM,1" "80%"   # E.g., ess1969
  155.     unmute_and_set_level "DAC" "80%"     # E.g., envy24, cs46xx
  156.     unmute_and_set_level "DAC,0" "80%"   # E.g., envy24
  157.     unmute_and_set_level "DAC,1" "80%"   # E.g., envy24
  158.     unmute_and_set_level "Synth" "80%"
  159.     unmute_and_set_level "CD" "80%"
  160.  
  161.     mute_and_zero_level "Mic"
  162.     mute_and_zero_level "IEC958"         # Ubuntu #19648
  163.  
  164.     # Intel P4P800-MX  (Ubuntu bug #5813)
  165.     switch_control "Master Playback Switch" on
  166.     switch_control "Master Surround" on
  167.  
  168.     # Trident/YMFPCI/emu10k1:
  169.     unmute_and_set_level "Wave" "80%"
  170.     unmute_and_set_level "Music" "80%"
  171.     unmute_and_set_level "AC97" "80%"
  172.  
  173.     # DRC:
  174.     unmute_and_set_level "Dynamic Range Compression" "80%"
  175.  
  176.     # Required for HDA Intel (hda-intel):
  177.     unmute_and_set_level "Front" "80%"
  178.  
  179.     # Required for SB Live 7.1/24-bit (ca0106):
  180.     unmute_and_set_level "Analog Front" "80%"
  181.  
  182.     # Required at least for Via 823x hardware on DFI K8M800-MLVF Motherboard with kernels 2.6.10-3/4 (see ubuntu #7286):
  183.     switch_control "IEC958 Capture Monitor" off
  184.  
  185.     # Required for hardware allowing toggles for AC97 through IEC958,
  186.     #  valid values are 0, 1, 2, 3. Needs to be set to 0 for PCM1.
  187.     unmute_and_set_level "IEC958 Playback AC97-SPSA" "0"
  188.  
  189.     # Required for newer Via hardware (see Ubuntu #31784)
  190.     unmute_and_set_level "VIA DXS,0" "80%"
  191.     unmute_and_set_level "VIA DXS,1" "80%"
  192.     unmute_and_set_level "VIA DXS,2" "80%"
  193.     unmute_and_set_level "VIA DXS,3" "80%"
  194.  
  195.     # Required on some notebooks with ICH4:
  196.     switch_control "Headphone Jack Sense" off
  197.     switch_control "Line Jack Sense" off
  198.  
  199.     # Some machines need one or more of these to be on;
  200.     # others need one or more of these to be off:
  201.     #
  202.     # switch_control "External Amplifier" on
  203.     switch_control "Audigy Analog/Digital Output Jack" on
  204.     switch_control "SB Live Analog/Digital Output Jack" on
  205.  
  206.     return 0
  207. }
  208.  
  209. # $1 <card ID> | "all"
  210. sanify_levels()
  211. {
  212.     TTSDML_RETURNSTATUS=0
  213.     case "$1" in
  214.       all)
  215.         for CARD in $(echo_card_indices) ; do
  216.             sanify_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  217.         done
  218.         ;;
  219.       *)
  220.         sanify_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  221.         ;;
  222.     esac
  223.     return $TTSDML_RETURNSTATUS
  224. }
  225.  
  226. # $1 <card ID>
  227. preinit_levels_on_card()
  228. {
  229.     CARDOPT="-c $1"
  230.  
  231.     # Silly dance to activate internal speakers by default on PowerMac
  232.     # Snapper and Tumbler
  233.     id=`cat /proc/asound/card$1/id 2>/dev/null`
  234.     if [ "$id" = "Snapper" -o "$id" = "Tumbler" ]; then
  235.         switch_control "Auto Mute" off
  236.         switch_control "PC Speaker" off
  237.         switch_control "Auto Mute" on
  238.     fi
  239. }
  240.  
  241. # $1 <card ID> | "all"
  242. preinit_levels()
  243. {
  244.     TTSDML_RETURNSTATUS=0
  245.     case "$1" in
  246.       all)
  247.         for CARD in $(echo_card_indices) ; do
  248.             preinit_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  249.         done
  250.         ;;
  251.       *)
  252.         preinit_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  253.         ;;
  254.     esac
  255.     return $TTSDML_RETURNSTATUS
  256. }
  257.  
  258. # $1 <card ID>
  259. mute_and_zero_levels_on_card()
  260. {
  261.     CARDOPT="-c $1"
  262.     for CTL in \
  263.         Master \
  264.         PCM \
  265.         Synth \
  266.         CD \
  267.         Line \
  268.         Mic \
  269.         "PCM,1" \
  270.         Wave \
  271.         Music \
  272.         AC97 \
  273.         "Master Digital" \
  274.         DAC \
  275.         "DAC,0" \
  276.         "DAC,1" \
  277.         Headphone \
  278.         Playback
  279.     do
  280.         mute_and_zero_level "$CTL"
  281.     done
  282. #    for CTL in \
  283. #        "Audigy Analog/Digital Output Jack" \
  284. #        "SB Live Analog/Digital Output Jack"
  285. #    do
  286. #        switch_control "$CTL" off
  287. #    done
  288.     return 0
  289. }
  290.  
  291. # $1 <card ID> | "all"
  292. mute_and_zero_levels()
  293. {
  294.     TTZML_RETURNSTATUS=0
  295.     case "$1" in
  296.       all)
  297.         for CARD in $(echo_card_indices) ; do
  298.             mute_and_zero_levels_on_card "$CARD" || TTZML_RETURNSTATUS=1
  299.         done
  300.         ;;
  301.       *)
  302.         mute_and_zero_levels_on_card "$1" || TTZML_RETURNSTATUS=1
  303.         ;;
  304.     esac
  305.     return $TTZML_RETURNSTATUS
  306. }
  307.  
  308.  
  309. # $1 <card ID> | "all"
  310. card_OK()
  311. {
  312.     [ "$1" ] || bugout
  313.     if [ "$1" = all ] ; then
  314.         [ -d /proc/asound ]
  315.         return $?
  316.     else
  317.         [ -d "/proc/asound/card$1" ] || [ -d "/proc/asound/$1" ]
  318.         return $?
  319.     fi
  320. }
  321.  
  322. # If a card identifier is provided in $2 then regard it as an error
  323. # if that card is not present; otherwise don't regard it as an error.
  324.  
  325. case "$1" in
  326.   start)
  327.     EXITSTATUS=0
  328.     TARGET_CARD="$2"
  329.     case "$TARGET_CARD" in
  330.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Setting up ALSA" ;;
  331.     esac
  332.     if ! card_OK "$TARGET_CARD"; then
  333.         [ "$TARGET_CARD" = "all" ] && log_action_end_msg "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  334.         exit $?
  335.     fi
  336.     preinit_levels "$TARGET_CARD" || EXITSTATUS=1
  337.     if ! restore_levels "$TARGET_CARD" ; then
  338.         sanify_levels "$TARGET_CARD" || EXITSTATUS=1
  339.         restore_levels "$TARGET_CARD" >/dev/null 2>&1 || :
  340.     fi
  341.     [ "$TARGET_CARD" = "all" ] && log_action_end_msg_and_exit "$EXITSTATUS"
  342.     exit $EXITSTATUS
  343.     ;;
  344.   stop)
  345.     EXITSTATUS=0
  346.     TARGET_CARD="$2"
  347.     case "$TARGET_CARD" in
  348.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Shutting down ALSA" ;;
  349.       *) log_action_begin_msg "Shutting down ALSA card ${TARGET_CARD}" ;;
  350.     esac
  351.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  352.     store_levels "$TARGET_CARD" || EXITSTATUS=1
  353.     mute_and_zero_levels "$TARGET_CARD" || EXITSTATUS=1
  354.     log_action_end_msg_and_exit "$EXITSTATUS"
  355.     ;;
  356.   restart|force-reload)
  357.     EXITSTATUS=0
  358.     $0 stop || EXITSTATUS=1
  359.     $0 start || EXITSTATUS=1
  360.     exit $EXITSTATUS
  361.     ;;
  362.   reset)
  363.     TARGET_CARD="$2"
  364.     case "$TARGET_CARD" in
  365.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Resetting ALSA" ;;
  366.       *) log_action_begin_msg "Resetting ALSA card ${TARGET_CARD}" ;;
  367.     esac
  368.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  369.     preinit_levels "$TARGET_CARD"
  370.     sanify_levels "$TARGET_CARD"
  371.     log_action_end_msg_and_exit "$?"
  372.     ;;
  373.   *)
  374.     echo "Usage: $MYNAME {start [CARD]|stop [CARD]|restart [CARD]|reset [CARD]}" >&2
  375.     exit 3
  376.     ;;
  377. esac
  378.  
  379.